package core.tests.resquests;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import junit.framework.Assert;
import codigoGerado.LexicalAnalyzer;
import core.exceptions.CompilerException;

public abstract class CompilerRequest {

	private final String FILES_PATH = "src/core/tests/files/";

	private LexicalAnalyzer lexicalAnalyzer;

	public CompilerRequest(String fileName) {
		try {
			this.lexicalAnalyzer = new LexicalAnalyzer(new FileReader(
					FILES_PATH + fileName));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public LexicalAnalyzer getLexer() {
		return lexicalAnalyzer;
	}

	public void analyze() {
		try {
			run();
		} catch (CompilerException sException) {
			Assert.fail("An unexpected exception was thrown while the Semantic Analyser was parsing the input: '"
					+ sException.getMessage() + "'");
		} catch (Exception e) {
			System.out.println(e.getMessage());
			Assert.fail("An IOException was thrown by the Analyser while analyzing the input.");
		}
	}

	public void analyze(CompilerException cException) {
		try {
			run();

			Assert.fail("The following expected exception did not occured: '"
					+ cException + "'");
		} catch (CompilerException lException) {
			Assert.assertEquals(lException, cException);
		} catch (IOException e) {
			Assert.fail("An IOException was thrown by the Analyser while analyzing the input.");
		} catch (Exception e) {
			e.printStackTrace();
			Assert.fail();
		}
	}

	protected abstract void run() throws Exception;

}
